Wallbox Pulsar plus charger lock/unlock pause/resume

So, through facebook I had a couple of requests to share what I’ve done.
To run this script you need your login information to your Wallbox account ( https://my.wallbox.com ) and the charger ID you want to lock or unlock.
Getting the ID is simple. Just login to my.wallbox.com and click on your charger. When you are on the page with all the details of your charger you can read the ID from the URL in your browser. It’s the number after the last slash.
wallbox_charger_nr

Go to homeyscript and create a new script. Call it e.g. wallbox ( I did ).
Copy and paste the code below in your new script and enter the details in the top three lines of the code.
Save it.

In my case I then created a virtual device called “laadpaal”.
afbeelding

And two flows for when turned on or off ( unlock or lock ).
afbeelding

To unlock your charger, run the wallbox script with argument unlock.
afbeelding

To lock your charger, run the wallbox script with argument lock.
afbeelding

If you have Google connected to your Homey, you can simply say zet laadpaal aan or set laadpaal uit to unlock or lock the charger.

Code :

const username = '<your username (usually email address)';
const password = '<your password>';
const MYCHARGERID = 000000; // ID of your charger

const BASEURL = "https://api.wall-box.com/";
const URL_AUTHENTICATION = 'auth/token/user';
const URL_UNLOCKCHARGER = 'v2/charger/';

var token = '';
var unlocked = { 'locked': 0 };
var locked = { 'locked': 1 };


/**
 * Get JWT Token from wallbox using login and password
 */
async function connectToWallbox() {
  let connect = await fetch( BASEURL + URL_AUTHENTICATION, {
    headers: {
      'Authorization': 'Basic ' + Buffer.from( username + ":" + password ).toString( 'base64' ),
      'Accept': 'application/json, text/plain, */*',
      'Content-Type': 'application/json;charset=utf-8',
    }
  } ).then( response => response.json() );
  return connect.jwt;
}

/**
 * Unlock wallbox function with token from Connect function
 */
async function unlockWallbox( token ) {
  let unlock = await fetch( BASEURL + URL_UNLOCKCHARGER + MYCHARGERID, {

    method: 'PUT',
    headers: {
      'Authorization': 'Bearer ' + token,
      'Accept': 'application/json, text/plain, */*',
      'Content-Type': 'application/json;charset=utf-8',

    },
    body: JSON.stringify( unlocked )
  } ).then( response => response.json() );
  if ( unlock.code === 200 ) {
    return false;
  }
  return true;
}

/**
 * Lock wallbox function with token from Connect function
 */
async function lockWallbox(token) {
  let lock = await fetch( BASEURL + URL_UNLOCKCHARGER + MYCHARGERID, {

    method: 'PUT',
    headers: {
      'Authorization': 'Bearer ' + token,
      'Accept': 'application/json, text/plain, */*',
      'Content-Type': 'application/json;charset=utf-8',

    },
    body: JSON.stringify(locked)
  } ).then( response => response.json() );
  if ( lock.code === 200 ) {
    return false;
  }
  return true;
}

// Make sure this script works with 1 single argument
if ( typeof args[0] !== 'string' ) {
  throw new Error( 'Script must be run from a flow with a string variable' );
}


switch ( args[0] ) {
  case "unlock":
    token = await connectToWallbox();
    result = await unlockWallbox( token );
    // Optional. You can leave the next 3 lines out and place them in a flow
    if ( result === true ) {
      say( 'Wallbox is ontgrendeld' );
    }
    break;
  case "lock":
    token = await connectToWallbox();
    result = await lockWallbox( token );
    // Optional. You can leave the next 3 lines out and place them in a flow
    if ( result === true ) {
      say( 'Wallbox is vergrendeld' );
    }
    break;
}
4 Likes